-
Notifications
You must be signed in to change notification settings - Fork 245
libm: define and implement trait NarrowingDiv
for unsigned integer division
#1011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8671beb
to
aa1fadc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thanks for including detailed comments. A few requests, mostly surface level
/// | ||
/// # Safety | ||
/// Requires that `n.leading_zeros() == 0` and `a < n`. | ||
unsafe fn div_three_digits_by_two<U>(a0: U, a: U::D, n: U::D) -> (U, U::D) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe div_three_3x_by_2x
or so? Just thinking that the inputs aren't really digits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are digits in base R = 2.pow(U::BITS)
, and this function does division of a 3-digit number by a 2-digit number in that base. Admittedly the signature is rather weird, passing the "high two thirds" of the 3-digit number as one U::D
and the least significant third as a U
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome!
86969de
to
9886f42
Compare
9886f42
to
d71f763
Compare
New utility in
libm::support
:trait NarrowingDiv
for dividingu2N / uN
when the quotient fits inuN
u256 / u128
This is the inverse operation of unsigned widening multiplication:
The trait API is based on x86's
div
-instruction: quotient overflow happens exactly when the high half of the dividend is greater or equal to the divisor, which includes division by zero.Split from #1002